package com.shenyue.nbtownlet.util;

import java.security.MessageDigest;
import java.util.Date;

/**
 * Created by 颖杰_2 on 2016-08-25.
 */
public class SecurityUtil {
    private static final String ENCODING = "utf-8";

    public static Long getTimestamp(){
        long t = new Date().getTime();
        long timestamp = (long)(Double.valueOf(String.valueOf(t)) / 1000);
        return timestamp;
    }

    public static String getToken(String APPKEY, String SECRETKEY, Long Timestamp) {
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(APPKEY).append(SECRETKEY).append(Timestamp);
            MessageDigest md = MessageDigest.getInstance("MD5");
            return StringUtil.byte2hex(md.digest(sb.toString().getBytes(ENCODING)));
        } catch (Exception e) {
            return null;
        }
    }

    public static String MD5(String message) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            return StringUtil.byte2hex(md.digest(message.getBytes(ENCODING)));
        } catch (Exception e) {
            return null;
        }
    }
}
